package club.banyuan;

import club.banyuan.util.Constant;
import club.banyuan.util.PropUtil;
import com.alibaba.fastjson.JSONObject;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class DeptService {
    private int idCount = 1;
    private List<Dept> deptList = new ArrayList<>();
    private static final DeptService INSTANCE = load();

    private DeptService() {
    }


    public int getIdCount() {
        return idCount;
    }

    public void setIdCount(int idCount) {
        this.idCount = idCount;
    }

    public List<Dept> getDeptList() {
        return deptList;
    }

    public void setDeptList(List<Dept> deptList) {
        this.deptList = deptList;
    }

    public static DeptService getINSTANCE() {
        return INSTANCE;
    }

    public void addDept(Dept dept) {
        Optional<Dept> any = deptList.stream().filter(t -> t.getName().equals(dept.getName()))
                .findAny();

        if (any.isPresent()) {
            throw new BadRequestException("部门已存在");
        }

        dept.setId(idCount++);
        deptList.add(dept);
        save();
    }

    public void deleteDept(int id) {
        deptList.removeIf(dept -> dept.getId() == id);
        save();
    }

    public Dept getDept(int id) {
        for (Dept dept : deptList) {
            if (dept.getId() == id) {
                return dept;
            }
        }
        throw new BadRequestException("部门不存在 id:" + id);
    }

    public void updateDept(Dept dept) {
        Dept old = getDept(dept.getId());
        old.setName(dept.getName());
        old.setDescription(dept.getDescription());
        save();
    }

    private void save() {
        String rlt = JSONObject.toJSONString(this);
        try (FileOutputStream fileOutputStream = new FileOutputStream(
                PropUtil.getProp(Constant.Prop.DEPT_STORE))) {
            fileOutputStream.write(rlt.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static DeptService load() {
        try (FileInputStream inputStream = new FileInputStream(PropUtil.getProp(Constant.Prop.DEPT_STORE))) {

            return JSONObject.parseObject(inputStream, StandardCharsets.UTF_8, DeptService.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return new DeptService();
    }

    public List<Dept> getRowsList(int page,int rows,String name){
        List<Dept> searchList;
        if (name==null||name.equals("")){
            searchList=getDeptList();
        }else{
            searchList=deptList.stream().filter(t->
                    t.getName().contains(name)).collect(Collectors.toList());
        }

        int total=searchList.size();
        List<Dept> rowsList=new ArrayList<>();

        int allPages = (int) Math.ceil(total * 1.0 / rows);
        System.out.println(allPages);
        DeptService deptService = HttpServer.getDeptService();
        if (allPages <= 1) {
            return searchList;
        }
        for (int i = 8 * (page - 1), j = 0; i < deptService.getDeptList().size() && j < 8; i++,j++) {
            rowsList.add(deptService.getDeptList().get(i));
        }
        return rowsList;
    }

    public static void main(String[] args) {
        DeptService deptService = new DeptService();
        Dept dept = new Dept();
        dept.setName("人事部");
        dept.setDescription("管理人事");
        deptService.addDept(dept);

        Dept dept1 = new Dept();
        dept1.setName("研发部");
        dept1.setDescription("管理研发");
        deptService.addDept(dept1);

        Dept dept3 = new Dept();
        dept3.setName("产品部");
        dept3.setDescription("管理产品");
        deptService.addDept(dept3);
    }
}
